using UnityEngine;
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;


// to be attached to a visual planet to allow it to run in orbit
public class PlanetOrbit : MonoBehaviour
{
	public GUISkin gSkin;
	public GUISkin altSkin;

	public bool toRun = true;
	public GameObject sun;
	public float speed = 0.0f;
	public Planet planet;
	public Vector3 axis = Vector3.up;
	public float self = 0.0f;

	private Rect rectWin1 = new Rect(10, 10, 320, 20);
	private Rect rectWin2 = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 30, 200, 60);
	private bool tip = false;
	private string plText = "";

	private bool toConfirm = false;				// to confirm the consumption of colonybase
	private bool toColony = false;
	private string ssettle = "";

	// Use this for initialization
	void Start ()
	{
		// planet's self-rotation speed
		self = Utility.RandomMinMax(45, 75);
	}
	
	// Update is called once per frame
	void Update ()
	{
		Transform cam = Core.oMainCam.transform;
		Vector3 pt = cam.transform.position - sun.transform.position;
		double distance = Math.Sqrt(Math.Pow(pt.x, 2) + Math.Pow(pt.y, 2) + Math.Pow(pt.z, 2));

		if (distance > 25.0)
			sun.active = false;
		else
			sun.active = true;

		// planet's self rotation
		if (toRun)
			transform.Rotate(Vector3.up * self * Time.deltaTime);
	}

	void LateUpdate()
	{
		if (toRun)
		{
			// planet's rotation around sun
			transform.RotateAround(sun.transform.position, axis, speed * Time.deltaTime);
			GetComponent<TrailRenderer>().enabled = true;
		}
	}

	void OnGUI()
	{
		GUI.skin = gSkin;

		if (tip)
		{
			// using raycast to detect clicked objects
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;

			if (Physics.Raycast(ray, out hit))
			{
				if (hit.transform == transform)
				{
					rectWin1 = GUILayout.Window(3, rectWin1, WinPlanetInfo, "Planet Info");

					// GUI.TextArea(new Rect(Camera.main.WorldToViewportPoint(transform.position + Vector3.up).x, Camera.main.WorldToViewportPoint(transform.position).y, 220, 160), plText, "box");
				}
			}
		}

		if (toConfirm)
			rectWin2 = GUILayout.Window(4, rectWin2, WinConfirmColony, ssettle);
	}

	void WinConfirmColony(int windowID)
	{
		GUILayout.BeginVertical(gSkin.box);

		GUILayout.Label("Planet: " + planet.PlanetName, altSkin.box);

		if (GUILayout.Button("OK", altSkin.button))
		{
			toConfirm = false;

			if (toColony)
			{
				toColony = false;
				Core.oGuiCam.GetComponent<GuiCamera>().isTop = false;
				((PlayerKingdom)Core.galaxyKingdoms[0]).SettleColonyShip(planet.Solar, planet);
			}
			else
			{
				Kingdom.RegisterNewColony(Core.galaxyKingdoms[0], planet, true);
				//ToPlanetView();
			}

			ToPlanetView();
			Core.planetHandle = false;
		}

		if (GUILayout.Button("Cancel", altSkin.button))
		{
			toConfirm = false;
			toColony = false;

			Core.planetHandle = false;
		}

		GUILayout.EndVertical();
		GUI.DragWindow();
	}

	void WinPlanetInfo(int windowID)
	{
		GUILayout.BeginVertical(gSkin.box);

		GUILayout.TextArea(plText);

		GUILayout.EndVertical();
	}

	void OnMouseEnter()
	{
		tip = true;
		plText = Planet.PreparePlanetText(planet);
	}

	void OnMouseExit()
	{
		tip = false;
	}

	void OnMouseDown()
	{
		if (Input.GetMouseButton(0))
		{
			// using raycast to detect clicked objects
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;

			if (Physics.Raycast(ray, out hit))
			{
				if (hit.transform == transform)
				{
					Debug.Log(transform.name);
				}
			}
		}
	}

	void OnMouseUp()
	{
		if (Core.planetHandle)
			return;									// another planet is under handling

		bool access = false;
		bool conquered = false;

		foreach (Kingdom.SolarPlanet sp in Core.galaxyKingdoms[0].Planets)
		{
			if (!planet.Giant && planet.Solar == sp.pSolar)				// planet can be found in reachable solar systems
			{
				access = true;

				if (planet.Status == Planet.PlanetStatus.Conquered)
					conquered = true;
			}
		}

		if (!access)
			return;

		// to switch camera to display planet contents
		if (conquered)
		{
			ToPlanetView();
			return;
		}

		// detect for colonybase
		if (planet.Solar.SolarColony.ContainsKey(Core.galaxyKingdoms[0]) && (int)planet.Solar.SolarColony[Core.galaxyKingdoms[0]] > 0)
		{
			ssettle = "Settle Colony Base";
			toConfirm = true;						// there are available new colony bases
		}
		else if (planet.Solar.OwnedBy.Contains(Core.galaxyKingdoms[0]) && planet.Status == Planet.PlanetStatus.None)				// detect for colony ship docked
		{
			int cnt = 0;
			foreach (Fleet fl in planet.Solar.Docking)
				if (fl is ColonyShip)
					cnt++;

			if (cnt > 0)
			{
				ssettle = "Settle Colony Ship";
				toConfirm = true;
				toColony = true;
			}
		}

		if (toConfirm || toColony)
			Core.planetHandle = true;


		GUILayer glayer = Camera.main.GetComponent<GUILayer>();

		if (null != glayer.HitTest(Input.mousePosition))
		{
			Debug.Log(glayer.HitTest(Input.mousePosition).name);
		}

	}

	void ToPlanetView()
	{
		Core.DisableMainCamera();

		Core.oAltCam.active = true;
		Core.oAltCam.GetComponent<StarCamera>().planet = planet;
	}
}
